import numpy as np
from numpy import *

# 查表得出各个情况的Xk和Ak
Xk = [
    [0.0000000],
    [-0.5773503, 0.5773503],
    [-0.7745967, 0.0000000, 0.7745967],
    [-0.8611363, -0.3399810, 0.3399810, 0.8611363],
    [-0.9061798, -0.5384693, 0.0000000, 0.5384693, 0.9061798],
    [-0.9324695, -0.6612094, -0.2386192, 0.2386192, 0.6612094, 0.9324695]
]

Ak = [
    [2.0000000],
    [1.0000000, 1.0000000],
    [0.5555556, 0.8888889, 0.5555556],
    [0.3478548, 0.6521452, 0.6521452, 0.3478548],
    [0.2369269, 0.4786287, 0.5688889, 0.4786287, 0.2369269],
    [0.1713245, 0.3607616, 0.4679139, 0.4679139, 0.3607616, 0.1713245]
]


def f(a, b, x):
    '''
    定义f(x)
    :param x: 需要计算的函数值
    :return: 返回sinx/x 的值
    '''
    t = (a + b) / 2 + (b - a) * x / 2
    return 1 / t


# 复合高斯公式
def Compound_Goss(point, begin=-1.0, end=1.0, n=1):
    # 根据传入参数确定是哪种高斯公式，选择对应的Xk和Ak值
    x = Xk[point - 1]
    A = Ak[point - 1]
    # 计算间隔h
    h = (end - begin) / n
    # print h
    I = np.zeros(n, dtype=np.float64)
    for i in range(n):
        a = begin + i * h
        b = begin + (i + 1) * h
        # print a,b
        I[i] = sum([f(a, b, x[k]) * A[k] for k in range(point)])
        I[i] *= (b - a) / 2
    return sum(I)


if __name__ == '__main__':
    print("结果为---", Compound_Goss(point=2, begin=1, end=3, n=4))
